﻿using log4net.Appender;
using log4net.Config;
using log4net.Layout;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Note
{
    public static class Logger
    {
        public static string LogFolder;
        public static string LogName;
        public static void LoggerInit(string path, string logName)
        {
            try
            {
                LogFolder = path + Path.DirectorySeparatorChar.ToString() + logName;
                LogName = LogFolder + Path.DirectorySeparatorChar.ToString() + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".txt";

                if (!Directory.Exists(LogFolder))
                {
                    Directory.CreateDirectory(LogFolder);
                }

                FileInfo[] arrFi = new DirectoryInfo(LogFolder).GetFiles();
                if (arrFi.Length > 50)
                {
                    Array.Sort(arrFi, (x, y) => x.CreationTime.CompareTo(y.CreationTime));

                    for (int i = 0; i < arrFi.Length - 50; i++)
                    {
                        try
                        {
                            //delete the oldest file.
                            arrFi[i].Delete();
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                        }
                    }
                }

                RollingFileAppender fileAppender = new RollingFileAppender();
                fileAppender.File = LogName;
                fileAppender.AppendToFile = true;
                //fileAppender.MaxFileSize = 2 * 1024 * 1024;


                PatternLayout patternLayout = new PatternLayout();
                patternLayout.ConversionPattern = "%date [%02thread] %-5level %logger: %message%newline";
                patternLayout.ActivateOptions();
                fileAppender.Layout = patternLayout;

                fileAppender.Encoding = Encoding.UTF8;

                fileAppender.ActivateOptions();
                BasicConfigurator.Configure(fileAppender);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Logger initialization fails and the error message is:\n" + ex.Message);
            }
        }

        public static void Info(Type t, string msg)
        {
            log4net.LogManager.GetLogger(t).Info(msg);
            System.Diagnostics.Debug.WriteLine(msg);
        }

        public static void Debug(Type t, string msg)
        {
            log4net.LogManager.GetLogger(t).Debug(msg);
            System.Diagnostics.Debug.WriteLine(msg);
        }

        public static void Warn(Type t, string msg)
        {
            log4net.LogManager.GetLogger(t).Warn(msg);
            System.Diagnostics.Debug.WriteLine(msg);
        }

        public static void Error(Type t, string msg)
        {
            log4net.LogManager.GetLogger(t).Error(msg);
            System.Diagnostics.Debug.WriteLine(msg);
        }
    }
}
